// pages/perchase-list/createlist/createlist.js
// index.js  
Page({  
  data: {  
    startTime: '',  
    endTime: '',  
    boardTypes: ['板子A', '板子B', '板子C'], // 假数据，实际使用时通过接口获取  
    hardwareSizes: ['五金1', '五金2', '五金3'], // 假数据，实际使用时通过接口获取  
    boardItems: [  
      { boardType: '', quantity: 0 }  
    ],  
    hardwareItems: [  
      { hardwareSize: '', quantity: 0 }  
    ]  
  },  
  
  // 开始采购时间选择  
  bindStartTimeChange: function(e) {  
    this.setData({  
      startTime: e.detail.value  
    });  
  },  
  
  // 结束采购时间选择  
  bindEndTimeChange: function(e) {  
    this.setData({  
      endTime: e.detail.value  
    });  
  },  
  
  // 板子类型选择改变  
  onBoardTypeChange: function(e) {  
    const index = e.currentTarget.dataset.index;  
    const boardTypes = this.data.boardTypes;  
    const value = e.detail.value;  
    const boardItems = this.data.boardItems;  
    boardItems[index].boardType = boardTypes[value];  
    this.setData({  
      boardItems: boardItems  
    });  
  },  
  
  // 板子数量输入改变  
  onBoardQuantityInput: function(e) {  
    const index = e.currentTarget.dataset.index;  
    const quantity = e.detail.value;  
    const boardItems = this.data.boardItems;  
    boardItems[index].quantity = quantity;  
    this.setData({  
      boardItems: boardItems  
    });  
  },  
  
  // 添加板子条目  
  addBoardItem: function() {  
    const boardItems = this.data.boardItems;  
    boardItems.push({ boardType: '', quantity: 0 });  
    this.setData({  
      boardItems: boardItems  
    });  
  },  
  
  // 删除板子条目  
  removeBoardItem: function(e) {  
    const index = e.currentTarget.dataset.index;  
    const boardItems = this.data.boardItems;  
    boardItems.splice(index, 1);  
    this.setData({  
      boardItems: boardItems  
    });  
  },  
  
  // 五金尺寸选择改变  
  onHardwareSizeChange: function(e) {  
    const index = e.currentTarget.dataset.index;  
    const hardwareSizes = this.data.hardwareSizes;  
    const value = e.detail.value;  
    const hardwareItems = this.data.hardwareItems;  
    hardwareItems[index].hardwareSize = hardwareSizes[value];  
    this.setData({  
      hardwareItems: hardwareItems  
    });  
  },  
  
  // 五金数量输入改变  
  onHardwareQuantityInput: function(e) {  
    const index = e.currentTarget.dataset.index;  
    const quantity = e.detail.value;  
    const hardwareItems = this.data.hardwareItems;  
    hardwareItems[index].quantity = quantity;  
    this.setData({  
      hardwareItems: hardwareItems  
    });  
  },  
  
  // 添加五金条目  
  addHardwareItem: function() {  
    const hardwareItems = this.data.hardwareItems;  
    hardwareItems.push({ hardwareSize: '', quantity: 0 });  
    this.setData({  
      hardwareItems: hardwareItems  
    });  
  },  
  
  // 删除五金条目  
  removeHardwareItem: function(e) {  
    const index = e.currentTarget.dataset.index;  
    const hardwareItems = this.data.hardwareItems;  
    hardwareItems.splice(index, 1);  
    this.setData({  
      hardwareItems: hardwareItems  
    });  
  },  
  
  // 提交采购单（此处仅为示例，实际提交应包含所有表单数据）  
  submitForm: function() {  
    // 此处可以添加提交表单的逻辑，如API调用等  
    wx.showToast({  
      title: '采购单已提交',  
      icon: 'success',  
      duration: 2000  
    });  
  }  
});